package org.anachronos.jeep.core.timer.impl;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

import org.anachronos.jeep.core.timer.Timer;

/**
 * Timer class that uses jmx beans provided by the JRE to measure 
 * execution time on a per thread base. Most exact way of measuring 
 * execution times, but might not be supported on some JDKs.
 * 
 * @author km
 */
public class ThreadMXTimer implements Timer {
	private final ThreadMXBean threadMXBean;
	
	public ThreadMXTimer() {
		threadMXBean = ManagementFactory.getThreadMXBean();
		assert threadMXBean.isThreadCpuTimeSupported();
		threadMXBean.setThreadCpuTimeEnabled(true);
	}
	
	/**
	 * {@inheritDoc}}
	 */
	public long nanoTime() {
		return threadMXBean.getCurrentThreadCpuTime();
	}
}
